import {reqcartlist,reqcartedit,reqcartdelete} from "../../http/api"
import { Toast } from "antd-mobile"
import { getUserInfo } from "./login"
let isRequest=true;
//state
const initState = {
    //1.1列表
    list:[],
    //2.1全选
    allChecked:false
}

//action type
const TYPES = {
    //1.2修改list type
    SHOP_CHANGE_LIST:"SHOP_CHANGE_LIST",
    //2.2修改全选
    SHOP_CHANGE_ALL_CHECKED:"SHOP_CHANGE_ALL_CHECKED"
}

//reducer
const reducer = (state = initState, action) => {
    switch (action.type) {
        //1.3修改list action
        case TYPES.SHOP_CHANGE_LIST:
            return{
                ...state,
                list:[...action.list]
            }
            case TYPES.SHOP_CHANGE_ALL_CHECKED:
                return{
                    ...state,
                    allChecked:action.allChecked
                }
        default:
            return state;
    }
}

//action creator
export const actions = {
    //1.4修改list action
    changeList:(list) => ({type:TYPES.SHOP_CHANGE_LIST,list}),
    //1.6请求list
    reqList:() => (dispatch,getState) =>{
        let userInfo=getUserInfo(getState())
        reqcartlist({uid:userInfo.uid}).then(res=>{
            let arr = res.data.list[1].content?res.data.list[1].content:[];
            let list = getList(getState())
            //补checked
            arr.forEach(item=>{
                let obj = list.find(i => i.id == item.id)
                item.checked = obj ? obj.checked : false
            })
            //修改list
            dispatch(actions.changeList(arr))
            //修改全选
            dispatch(actions.changeAllChecked(arr.length > 0 ? arr.every(item => item.checked) : false))
            //再次打开请求
            isRequest=true;
        })
    },
    //数量+
    add:id=>(dispatch,getState)=>{
        reqcartedit({
            id,
            type:2
        }).then(res=>{
            if(res.data.code===200){
                Toast.show({
                    content:res.data.msg
                })
                //请求列表
                dispatch(actions.reqList())
            }
        })
    },
    //数量—
    sub:(id,num)=>(dispatch,getState)=>{
        if(num<=1){
            Toast.show({
                content:"宝贝不能再少了"
            })
            return;
        }
        if(isRequest){
            reqcartedit({
                id,
                type:1
            }).then(res=>{
                Toast.show({
                    content:res.data.msg
                })
                //请求列表
                dispatch(actions.reqList())    
            })
        }
       
    },
    // 删除
    del:(id)=>dispatch=>{
        reqcartdelete({id}).then(res=>{
            Toast.show({
                content:res.data.msg
            })
            //请求列表
            dispatch(actions.reqList()) 
        })
    },
    //修改全选
    changeAllChecked:allChecked=>({type:TYPES.SHOP_CHANGE_ALL_CHECKED,allChecked}),
    //点击全选
    changeAll:()=>(dispatch,getState)=>{
        let allChecked=getAllChecked(getState());
        let newBool=!allChecked;
        //修改全选
        dispatch(actions.changeAllChecked(newBool));
        //修改list
        let list=getList(getState());
        list.forEach(item=>item.checked=newBool)
        dispatch(actions.changeList(list))
    },
    //点击某一个
    changeOne:index=>(dispatch,getState)=>{
        let list=getList(getState());
        list[index].checked=!list[index].checked;
        dispatch(actions.changeList(list));
        //修改全选
        dispatch(actions.changeAllChecked(list.every(item => item.checked)))
    }
}

//selector导出数据给组件
//1.5导出
export const getList = state => state.shop.list;
//导出allchecked
export const getAllChecked = state => state.shop.allChecked;
//总价
export const getAllPrice = state => {
    let list = getList(state);
    return list.reduce(
        (v, i) => (i.checked ? v + i.price * i.num : v),
        0
    )
}
//导出reducer
export default reducer;